//
//  JCClassDescription.m
//  JCSDK
//
//  Created by master on 16/2/23.
//  Copyright © 2016年 AdMaster. All rights reserved.
//

#import "JCClassDescription.h"
#import "JCPropertyDescription.h"

@interface JCClassDescription ()
{
    NSArray *_propertyDescriptions;
}

@end

@implementation JCClassDescription

- (instancetype)initWithSuperclassDescription:(JCClassDescription *)superclassDescription dictionary:(NSDictionary *)dictionary {

    if(self = [super initWithDictionary:dictionary]) {
        NSString *k_properties = @"properties";
        _superclassDescription = superclassDescription;
        NSMutableArray *propertyDescriptions = [[NSMutableArray alloc] initWithCapacity:[dictionary[k_properties] count]];
        
        for(NSDictionary *propertyDictionary  in dictionary[k_properties]) {
            JCPropertyDescription *propertyDescription = [[JCPropertyDescription alloc] initWithDictionary:propertyDictionary];
            [propertyDescriptions addObject:propertyDescription];
        }
        _propertyDescriptions = propertyDescriptions;
        
        
        _propertyDescriptions = [propertyDescriptions copy];
        
        NSMutableArray *delegateInfos = [NSMutableArray array];
        for (NSDictionary *delegateInfoDictionary in dictionary[@"delegateImplements"]) {
            [delegateInfos addObject:[[JCDelegateInfo alloc] initWithDictionary:delegateInfoDictionary]];
        }
        _delegateInfos = delegateInfos;
    }
    return self;
}


//TODO: (完成)需要修改不停查找父类的classDescription 例如UILabel 需要将他父类UIView的部分属性获取下来
- (NSArray *)propertyDescriptions {
//    return _propertyDescriptions;
    NSMutableDictionary *allProperties= [NSMutableDictionary dictionary];
    JCClassDescription *description = self;
    while (description) {
        for(JCPropertyDescription *propertyDescription in description->_propertyDescriptions) {
            if(allProperties[propertyDescription.name] == nil) {
                [allProperties setValue:propertyDescription forKey:propertyDescription.name];
            }
        }
        description = description.superclassDescription;
    }
    return [allProperties allValues];
}

@end



@implementation JCDelegateInfo

- (instancetype)initWithDictionary:(NSDictionary *)dictionary {
    if(self = [super init]) {
        _selector = dictionary[@"selector"];

    }
    return self;
}

@end
